clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_final"
global Output "Y:/limited/Michigan_CTE/funding_change/output/figures"

global c1 "31 119 180"
global c2 "255 127 14"
global c3 "44 160 44"
global c4 "214 39 40"

local demographics "female black hisp another_race sped lep econdis"
local academics "test_avg_g8 test_avg_sq_g8 test_avg_g8_imp attendance_g8 attendance_g8_imp"

// LOAD DATA
use $Input/compiled_student_analysis_data.dta

keep if grad_year >= 2012

replace c2014 = 0

// RUN REGRESSIONS
gen cohort = .
gen part0_over_time = .
gen part0_over_time_l95 = .
gen part0_over_time_u95 = .
gen conc0_over_time = .
gen conc0_over_time_l95 = .
gen conc0_over_time_u95 = .
gen comp0_over_time = .
gen comp0_over_time_l95 = .
gen comp0_over_time_u95 = .
gen part3_over_time = .
gen part3_over_time_l95 = .
gen part3_over_time_u95 = .
gen conc3_over_time = .
gen conc3_over_time_l95 = .
gen conc3_over_time_u95 = .
gen comp3_over_time = .
gen comp3_over_time_l95 = .
gen comp3_over_time_u95 = .
gen part5_over_time = .
gen part5_over_time_l95 = .
gen part5_over_time_u95 = .
gen conc5_over_time = .
gen conc5_over_time_l95 = .
gen conc5_over_time_u95 = .
gen comp5_over_time = .
gen comp5_over_time_l95 = .
gen comp5_over_time_u95 = .
gen part10_over_time = .
gen part10_over_time_l95 = .
gen part10_over_time_u95 = .
gen conc10_over_time = .
gen conc10_over_time_l95 = .
gen conc10_over_time_u95 = .
gen comp10_over_time = .
gen comp10_over_time_l95 = .
gen comp10_over_time_u95 = .

foreach rank in 0 3 5 10 {
	
  eststo: reghdfe cte_any_rankfact`rank' c2012-c2019 `demographics' `academics', a(school) vce(cluster school)
  local i = 1
  forvalues x = 2012/2019 {
    replace part`rank'_over_time = 100 * _b[c`x'] in `i'
    replace part`rank'_over_time_l95 = 100 * _b[c`x'] - (invttail(e(df_r),0.025) * 100 * _se[c`x']) in `i'
    replace part`rank'_over_time_u95 = 100 * _b[c`x'] + (invttail(e(df_r),0.025) * 100 * _se[c`x']) in `i'
    replace cohort = `x' in `i'
    local ++i
  }
  
  eststo: reghdfe cte_conc_comp_rankfact`rank' c2012-c2019 `demographics' `academics', a(school) vce(cluster school)
  local i = 1
  forvalues x = 2012/2019 {
    replace conc`rank'_over_time = 100 * _b[c`x'] in `i'
    replace conc`rank'_over_time_l95 = 100 * _b[c`x'] - (invttail(e(df_r),0.025) * 100 * _se[c`x']) in `i'
    replace conc`rank'_over_time_u95 = 100 * _b[c`x'] + (invttail(e(df_r),0.025) * 100 * _se[c`x']) in `i'
    local ++i
  }

  eststo: reghdfe cte_comp_rankfact`rank' c2012-c2019 `demographics' `academics', a(school) vce(cluster school)
  local i = 1
  forvalues x = 2012/2019 {
    replace comp`rank'_over_time = 100 * _b[c`x'] in `i'
    replace comp`rank'_over_time_l95 = 100 * _b[c`x'] - (invttail(e(df_r),0.025) * 100 * _se[c`x']) in `i'
    replace comp`rank'_over_time_u95 = 100 * _b[c`x'] + (invttail(e(df_r),0.025) * 100 * _se[c`x']) in `i'
    local ++i
  }
}

keep cohort part0* part3* part5* part10* ///
            conc0* conc3* conc5* conc10* ///
            comp0* comp3* comp5* comp10* 
drop if missing(cohort)


// DRAW COMPLETION GRAPH
tw (rarea comp0_over_time_l95  comp0_over_time_u95  cohort, color("$c4%40") lwidth(none)) ///
   (rarea comp3_over_time_l95  comp3_over_time_u95  cohort, color("$c2%40") lwidth(none)) ///
   (rarea comp5_over_time_l95  comp5_over_time_u95  cohort, color("$c3%40") lwidth(none)) ///
   (rarea comp10_over_time_l95 comp10_over_time_u95 cohort, color("$c1%40") lwidth(none)) ///
   (connected comp0_over_time comp3_over_time comp5_over_time comp10_over_time cohort, sort ///
   mc("$c4" "$c2" "$c3" "$c1") ///
   m(S T D O) ///
   lc("$c4" "$c2" "$c3" "$c1") ///
   lp(shortdash dash longdash solid)), ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(13.6 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(14.4 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2012/2019) ///
   xtitle("Cohort Graduation Year", margin(t=2)) ///
   ylabel(-5(5)15, angle(0) glc(gs13) glw(thin) glp(shortdash) gmax) ///
   ytitle("Percentage Point Change") ///
   legend(order(5 "Rank factor = 1" 6 "Rank factor = 2.5" 7 "Rank factor = 5" ///
     8 "Rank factor = 10") rows(2) symxsize(7) region(style(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7)   ///
	 scheme(s2color)


// EXPORT GRAPH
graph save $Output/A02breg_completion_by_rank.gph, replace
